package com.heima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.heima.common.result.PageResult;
import com.heima.common.result.Result;
import com.heima.pojo.dto.SkuDTO;
import com.heima.pojo.entity.Sku;
import com.heima.pojo.vo.SkuVO;
import com.heima.pojo.vo.Top10SkuVO;
import com.heima.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/8/15  22:11
 * \* Description:商品管理接口
 * \
 */
@RestController
@RequestMapping("api/vm-service/sku")
@Slf4j
public class SkuController {

    @Autowired
    private SkuService skuService;

    /**
     * 商品列表
     * @param skuDTO
     * @return
     */
    @GetMapping("search")
    public PageResult<SkuVO> page(SkuDTO skuDTO){
        return skuService.page(skuDTO);
    }

    /**
     * 新增商品
     * @param sku
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Sku sku){
        log.info("新增商品{}",sku);
        skuService.add(sku);
        return Result.success();
    }

    /**
     * 文件上传
     * @param fileName
     * @return
     */
    @PostMapping("fileUpload")
    public String fileUpload(@RequestParam("fileName") MultipartFile fileName){
        return skuService.fileUpload(fileName);
    }

    /**
     * 修改商品
     * @param skuId
     * @param sku
     * @return
     */
    @PutMapping("/{skuId}")
    public Result update(@PathVariable String skuId ,@RequestBody Sku sku){
        log.info("修改商品{}",skuId);
        sku.setSkuId(skuId);
        skuService.update(sku);
        return Result.success();
    }

    /**
     * 根据商圈id查询top10商品
     * @param businessId
     * @return
     */
    @GetMapping("/businessTop10/{businessId}")

    public List<Top10SkuVO> businessTop10(@PathVariable String businessId){
        log.info("查询商家{}的top10商品",businessId);
        return skuService.businessTop10(businessId);
    }

}
